package com.yunsc.shop.controller.shop;

import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.media.entity.YunscUploadFileEntity;
import com.yunsc.media.service.UploadService;
import com.yunsc.order.dto.RechargeDTO;
import com.yunsc.order.sevice.YunscPaymentService;
import com.yunsc.order.vo.RechargeVO;
import com.yunsc.shop.utils.ClientInfoUtils;
import com.yunsc.store.entity.YunscExpressEntity;
import com.yunsc.store.entity.YunscHelpEntity;
import com.yunsc.store.service.*;
import com.yunsc.store.vo.PageDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-05-04 0:38
 */
@Api(value = "店铺接口", tags = "StoreLoginController")
@Slf4j
@RestController()
public class ShopController {

    @Resource
    private YunscStoreSettingService storeSettingService;
    @Resource
    private YunscStoreService storeService;
    @Resource
    private YunscPageService pageService;
    @Resource
    private YunscHelpService helpService;
    @Resource
    private YunscExpressService expressService;
    @Resource
    private ClientInfoUtils clientInfoUtils;
    @Resource
    private YunscPaymentService paymentService;
    @Resource
    private UploadService uploadService;

    // /setting/data
    @ApiOperation(value = "获取店铺设置信息", tags = "StoreLoginController")
    @GetMapping("/setting/data")
    public Result<?> getShopSetting() {
        Integer storeId = clientInfoUtils.getCurrentStoreId();
        Map<String, Object> setting = storeSettingService.queryStoreSettingAll(storeId);
        Map<String, Object> result = new HashMap<>();
        result.put("setting", setting);
        return Result.ok(result);
    }

    // store/data
    @ApiOperation(value = "获取店铺信息", tags = "StoreLoginController")
    @GetMapping("/store/data")
    public Result<?> getShopData(HttpServletRequest request) {
        Integer storeId = clientInfoUtils.getCurrentStoreId();
        String platform = clientInfoUtils.getCurrentPlatform();
        Map<String, Object> store = storeService.queryStoreDataByPlatform(platform, storeId);
        Map<String, Object> result = new HashMap<>();
        result.put("store", store);
        return Result.ok(result);
    }

    // page/detail&pageId=0
    @ApiOperation(value = "获取店铺详情页信息", tags = "StoreLoginController")
    @GetMapping("/page/detail")
    public Result<?> getShopPageDetail(@RequestParam("pageId") Integer pageId) {
        Integer storeId = clientInfoUtils.getCurrentStoreId();
        PageDetailVO pageDetail = pageService.queryDefaultPageDetail(pageId, storeId);
        Map<String, Object> result = new HashMap<>();
        result.put("pageData", pageDetail);
        return Result.ok(result);
    }

    @ApiOperation(value = "获取帮助中心列表", tags = "StoreLoginController")
    @GetMapping("/help/list")
    public Result<?> queryHelpList(@RequestParam("page") Integer page) {
        Integer storeId = clientInfoUtils.getCurrentStoreId();
        PageResult<YunscHelpEntity> list = helpService.queryHelpList(page, storeId);
        return Result.resultPageList(list);
    }

    // /express/list
    @ApiOperation(value = "获取快递公司列表", tags = "StoreLoginController")
    @GetMapping("/express/list")
    public Result<?> getExpressList() {
        Integer storeId = clientInfoUtils.getCurrentStoreId();
        List<YunscExpressEntity> expressList = expressService.queryExpressList(storeId);
        return Result.resultList(expressList);
    }


    // /recharge/center&client=H5
    @ApiOperation(value = "获取充值中心信息", tags = "StoreLoginController")
    @GetMapping("/recharge/center")
    public Result<?> getRechargeCenter(@RequestParam("client") String client) {
        Integer storeId = clientInfoUtils.getCurrentStoreId();
        Integer userId = SecurityUtil.getCurrentUserIdEx();
        RechargeVO result = paymentService.queryRechargeCenter(client,userId, storeId);
        return Result.ok(result);
    }

    // /recharge/submit

    @ApiOperation(value = "充值提交", tags = "StoreLoginController")
    @PostMapping("/recharge/submit")
    public Result<?> rechargeSubmit(@RequestBody RechargeDTO recharge) {
        Integer storeId = clientInfoUtils.getCurrentStoreId();
        Integer userId = SecurityUtil.getCurrentUserIdEx();
        // paymentService.rechargeSubmit(recharge, userId, storeId);
        return Result.error("充值接口暂未开通，请联系客服充值");
    }


    @PostMapping("upload/image")
    public Result<?> uploadImage(HttpServletRequest httpServletRequest) throws IOException {
        Integer userId = SecurityUtil.getCurrentUserIdEx();
        Integer storeId = clientInfoUtils.getCurrentStoreId();
        // 获取MultiPart请求
        MultipartHttpServletRequest multiPartRequest = (MultipartHttpServletRequest) httpServletRequest;
        // 获取所有上传的文件
        Map<String, MultipartFile> fileMap = multiPartRequest.getFileMap();
        MultipartFile file = fileMap.get("file");
        // 上传文件、上传来源(10商户后台 20用户端)
        YunscUploadFileEntity uploadFile = uploadService.
                uploadFileEntity(file, 0, 20, null, userId, storeId);
        Map<String, Object> data = new HashMap<>();
        data.put("fileInfo", uploadFile);
        return Result.ok(data);
    }

    // @Resource
    // private YunscAdminUserDao adminUserDao;
    // @GetMapping("/test/date")
    // public Result<?> testDate() {
    //     List<YunscAdminUserEntity> yunscAdminUserEntities = adminUserDao.selectList(null);
    //     log.info("testDate:{}", System.currentTimeMillis()/1000);
    //     return Result.ok(yunscAdminUserEntities);
    // }
}
